//
//  MyReservationCell.swift
//  koolcar
//
//  Created by xiantian on 2023/10/30.
//

import UIKit

class MyReservationCell: UITableViewCell {
    private var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = .white
        tempView.layer.cornerRadius = 8
        tempView.layer.masksToBounds = true
        return tempView
    }()
    private var logoImgView: UIImageView = {
        let tempImgView = UIImageView()
        tempImgView.image = UIImage.init(named: "server_tip")
        tempImgView.contentMode = .scaleAspectFit
        tempImgView.clipsToBounds = true
        return tempImgView
    }()
    
    private var titleLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        tempLab.text = MMLocalizedString("mainten_server", comment: "服务")
        return tempLab
    }()
    
    private var statusLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .center
        //tempLab.text = "未到店"
        return tempLab
    }()
    
    
    private var imgView: UIImageView = {
        let tempImgView = UIImageView()
        tempImgView.image = UIImage.init(named: "service_success")
        tempImgView.contentMode = .scaleAspectFit
        return tempImgView
    }()
    
    private var nameLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    private var serviceTimeLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    private var brandNumLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    private var createTimeLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.placeholderColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var cancelBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.layer.borderWidth = 1
        tempBtn.layer.borderColor = Theme.titleLabColor.cgColor
        tempBtn.setup_titleColor(color: Theme.titleLabColor)
        tempBtn.setup_titleLabel(text: MMLocalizedString("my_reserver_cancel_reserver", comment: "取消预约"))
        tempBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        tempBtn.layer.cornerRadius = 8
        tempBtn.layer.masksToBounds = true
        tempBtn.backgroundColor = .white
        return tempBtn
    }()
    
    var orderData: MyReservationContentModel?
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = Theme.viewBkgGrayColor
        self.contentView.addSubview(self.bkgView)
        //头部
        bkgView.addSubview(logoImgView)
        bkgView.addSubview(titleLab)
        bkgView.addSubview(statusLab)
        
        //中间视图
        bkgView.addSubview(imgView)
        bkgView.addSubview(nameLab)
        bkgView.addSubview(serviceTimeLab)
        bkgView.addSubview(createTimeLab)
        bkgView.addSubview(brandNumLab)
        //取消按钮
        bkgView.addSubview(cancelBtn)
        self.cancelBtn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(10)
            make.right.equalTo(-14)
            make.bottom.equalTo(0)
        }
        logoImgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(22)
            make.width.equalTo(16)
            make.height.equalTo(16)
        }
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(logoImgView.snp.right).offset(2)
            make.top.equalTo(20)
            make.height.greaterThanOrEqualTo(20)
        }
        statusLab.snp.makeConstraints { make in
            make.left.equalTo(titleLab.snp.right).offset(10)
            make.top.equalTo(20)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-14)
        }
        
        imgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(titleLab.snp.bottom).offset(10)
            make.width.equalTo(90)
            make.height.equalTo(68)
        }
        
        nameLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(imgView.snp.top)
            make.height.greaterThanOrEqualTo(22)
            make.right.equalTo(-14)
        }
        
        serviceTimeLab.snp.makeConstraints { make in
            make.left.equalTo(nameLab.snp.left)
            make.top.equalTo(nameLab.snp.bottom).offset(4)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-14)
        }
        brandNumLab.snp.makeConstraints { make in
            make.left.equalTo(nameLab.snp.left)
            make.top.equalTo(serviceTimeLab.snp.bottom).offset(4)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-14)
        }
        
        createTimeLab.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.height.greaterThanOrEqualTo(22)
            make.top.greaterThanOrEqualTo(imgView.snp.bottom).offset(12).priority(.high)
//            make.top.greaterThanOrEqualTo(brandNumLab.snp.bottom).offset(12)
            make.right.equalTo(-14)
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.height.equalTo(40)
            make.width.equalTo(140)
            make.top.equalTo(createTimeLab.snp.bottom).offset(10)
            make.bottom.equalTo(-20)
        }
    }
    
    ///取消预约事件
    @objc func cancelBtnAction(){
        self.orderData?.cancelBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///预约状态 status 1：预约中 2：用户自己预约取消 3.后台取消 4，未使用 5已使用 6未到店
    func setMyReservationCell(data: MyReservationContentModel) {
        self.orderData = data
        self.nameLab.text = data.serviceName ?? ""
        self.serviceTimeLab.text = String(format: "%@ %@ %@",
                                          MMLocalizedString("my_reserver_time", comment: "预约时间"),
                                          (data.joinDate ?? "").substring(to: 10),
                                          data.startTime ?? "")
        self.brandNumLab.text = String(format: "%@：%@",
                                       MMLocalizedString("my_reserver_car_number", comment: "车牌号"),
                                       data.carNumber ?? "")
        self.createTimeLab.text = String(format: "%@", data.createdTime ?? "")
        
        if let url = URL.init(string: data.urlPicture ?? "") {
            self.imgView.sd_setImage(with: url, placeholderImage: UIImage.init(named: "icon_default_place_v"))
        }else{
            self.imgView.image = UIImage.init(named: "icon_default_place_v")
        }
        
        self.statusLab.textColor = Theme.titleLabColor
        if data.status == 4 {
            self.cancelBtn.isHidden = false
            self.cancelBtn.snp.updateConstraints { make in
                make.height.equalTo(40)
                make.top.equalTo(createTimeLab.snp.bottom).offset(10)
            }
            self.statusLab.text = MMLocalizedString("my_reserver_status4", comment: "未使用")
            self.statusLab.textColor = Theme.mainColor
        }else{
            self.cancelBtn.isHidden = true
            self.cancelBtn.snp.updateConstraints { make in
                make.height.equalTo(0)
                make.top.equalTo(createTimeLab.snp.bottom).offset(0)
            }
            switch data.status ?? 0 {
                case 2:
                    self.statusLab.text = MMLocalizedString("my_reserver_status2", comment: "已取消")
                case 3:
                    self.statusLab.text = MMLocalizedString("my_reserver_status2", comment: "已取消")
                case 4:
                    self.statusLab.text = MMLocalizedString("my_reserver_status4", comment: "未使用")
                    self.statusLab.textColor = Theme.mainColor
                case 5:
                    self.statusLab.text = MMLocalizedString("my_reserver_status5", comment: "已使用")
                case 6:
                    self.statusLab.text = MMLocalizedString("my_reserver_status6", comment: "未到店")
                default:
                    self.statusLab.text = MMLocalizedString("my_reserver_status4", comment: "未使用")
                    self.statusLab.textColor = Theme.mainColor
            }
        }
        self.updateConstraints()
    }
    
}
